package il.ac.technion.wiki.impl.search;


import il.ac.technion.wiki.api.search.Hit;
import il.ac.technion.wiki.api.wikidocument.WikiDocument;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class MyIndexSearcherTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {

	}

	@Test
	public void testGetTermHits() {
		WikiIndexSearcher mis = new WikiIndexSearcher();
		createDocs1(mis);
		List<Hit> hits = mis.getTermHits("titlea");
		Assert.assertEquals(1, hits.size());
		Hit hit = null;
		hit = hits.get(0);
		Assert.assertEquals("titlea",hit.docTitle);
		
		hits = mis.getTermHits("b");
		Assert.assertEquals(1, hits.size());
		hit = null;
		hit = hits.get(0);
		Assert.assertEquals("titleb",hit.docTitle);
		hits = mis.getTermHits("new york");
		Assert.assertEquals(3, hits.size());
		hit = null;
		Collections.sort(hits);
		hit = hits.get(0);
		Assert.assertEquals(1,hit.getScore());
		hit = hits.get(1);
		Assert.assertEquals(1,hit.getScore());
		hit = hits.get(2);
		Assert.assertEquals(2,hit.getScore());
	}
	
	@Test 
	public void testGetAllDocsHits(){
		WikiIndexSearcher mis = new WikiIndexSearcher( );
		createDocs2(mis);
		List<Hit> hits = mis.getAllDocsHits();
		Collections.sort(hits);
		Iterator<Hit> iter = hits.iterator();
		for(char i = 'a'; i <= 'g' ;i++ ){
			Hit hit = iter.next();
			String s = "" +  i;
			Assert.assertEquals("title" + s + "not equal to: " + hit.getTitle(),
					"title" + s, hit.getTitle());
		}
		Assert.assertFalse(iter.hasNext());
	}
	
	private void createDocs1(WikiIndexSearcher mis){
		for(char i = 'a'; i <= 'g' ;i++ ){
			String s = "" +  i;
			mis.addDocument(new WikiDocument("title" + s, s));
		}
		mis.addDocument(new WikiDocument("5" , "new york"));
		mis.addDocument(new WikiDocument("6" , "new new york"));
		mis.addDocument(new WikiDocument("7" , "new york, new york"));
	}
	private void createDocs2(WikiIndexSearcher mis){
		for(char i = 'a'; i <= 'g' ;i++ ){
			String s = "" +  i;
			mis.addDocument(new WikiDocument("title" + s, s));
		}
	}

}
